/**
 * @author Ahammer
 * @date 2009 05
 */

/*
 * This file is part of Iqm.
 * Copyright (c) 2010-2011 Helmut Ahammer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package opgui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import javax.media.jai.ParameterBlockJAI;
import javax.media.jai.PlanarImage;
import javax.swing.ButtonGroup;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import main.IqmTools;
import main.Look;
import main.Manager;
import main.Tank;


public class IqmTempMatchGUI  extends IqmOperatorJFrame
							  implements IqmOperatorGUIBuilder, ActionListener, AdjustmentListener {

	private  ParameterBlockJAI    pb         = null;  //  @jve:decl-index=0:
	//public  ImgProcFuncJFrame   opGUIFrame = null;
	//private  IqmTempMatchGUI 	  opGUIFrame = null;  //actionPerformed events werden in dieser Klasse abgehandelt
	
		
	private  JRadioButtonMenuItem buttRast			= null;
	private  JRadioButtonMenuItem buttIter			= null;
	private  JRadioButtonMenuItem buttMethodHidden	= null;
	private  JToolBar 			  jToolBarMethod	    = null;
	private  ButtonGroup 		  buttGroupMethod		= null;
	
	
	/**
	 * constructor
	 */
	public IqmTempMatchGUI() {

	}

	
	/**
	 * This method sets all  variables to null
	 */
	private void resetVariables(){
		pb         = null; 
		//opGUIFrame = null; 
		
		//jToolBarRGB  = null;

	}
	/**
	 * This method sets all parameters to null
	 */
	private void resetParameters(){
		//butt       = null;
		//butt       = null;
	}

	/**
	 * This method sets the current parameter block to the argument
	 * @param ParameterBlockJAI pb
	 */
	@Override
	public void setParameterBlock(ParameterBlockJAI arg){
		pb = arg;
		IqmTools.setCurrParameterBlockJAI(pb);	
	}
	/**
	 * This method sets the current parameter block
	 * The individual values of the GUI  current ParameterBlock
	 *
	 */
	@Override
	public void setParameterBlock(){

		if (buttRast.isSelected())  	pb.setParameter("Method", 0);
		if (buttIter.isSelected())     	pb.setParameter("Method", 1);

		IqmTools.setCurrParameterBlockJAI(pb);	
	}
	
	/**
	 * This method sets the current parameter values
	 *
	 */
	@Override
	public void setParameterValues(){
		int idx = IqmTools.getCurrManagerImgIndex();
	    PlanarImage pi = Tank.getCurrentTankImageAt(idx); 
	       if (pi == null){
	    	   System.out.println("IqmTempMatchGUI: Current image not defined");
	    	   return;
	       }
//	    int pixelSize = pi.getColorModel().getPixelSize();
//      int numBands  = pi.getNumBands(); 
//      String type = IqmTools.getCurrImgTyp(pixelSize, numBands);
        //-------------------------------------------------

       if (pb.getIntParameter("Method") == 0)     buttRast.setSelected(true);
       if (pb.getIntParameter("Method") == 1)     buttIter.setSelected(true);
       	}

	/**
	 * This method creates and shows the GUI
	 */
	@Override
	public void createIqmOperatorGUI() {
		//System.out.println("IqmTempMatch: createAndShowGUI invoked");
		//opGUIFrame = new IqmTempMatchGUI();      //actionPerforemd events werden in dieser Klasse abgehandelt
		//opGUIFrame.getJContentPane();
		this.initialize(); //Aufruf von ImgProcFuncJFrame
		this.setTitle("TempMatch");
		//PlanarImage pi = Tank.getCurrentTankImageAt(IqmTools.getCurrManagerImgIndex());
		//Dimension dim = this.getSize();
		//this.setSize(dim.width, dim.height+0);
		//this.setPreferredSize(new Dimension(800, 600));

		this.getJContentPane().add(getJToolBarMethod(),        getGridBagConstraintsMethod());
	
		this.pack();
		//this.repaint();
		if (pb == null) pb = new ParameterBlockJAI("iqmTempMatch");  //nur wenn das erste mal aufgerufen
		//pb = new ParameterBlockJAI("iqmthreshold"); //immer der default Block
		IqmTools.setCurrParameterBlockJAI(pb);
		this.setParameterValues();
	}

	private  GridBagConstraints getGridBagConstraintsMethod(){
		GridBagConstraints gridBagConstraintsMethod = new GridBagConstraints();
		gridBagConstraintsMethod.gridx = 1;
		gridBagConstraintsMethod.gridy = 4;
		gridBagConstraintsMethod.gridwidth = 3;
		gridBagConstraintsMethod.insets = new Insets(15,0,20,0);

		//gridBagConstraintsMethod.anchor = GridBagConstraints.LINE_START;
		//gridBagConstraintsMethod.fill = GridBagConstraints.BOTH;
		return gridBagConstraintsMethod;
	}
	

	//------------------------------------------------------------------------------------------------------------------
	/**
	 * This method closes and destroys the GUI
	 * 
	 * @return void
	 */
//	public void destroyGUI() {
//		if (this != null){
//					
//			//this.closeAndDestroyGUI();
//			//resetVariables();
//			//folgendes geht auch!?
//			this.setVisible(false);
//			this.dispose();
//			//this = null;
//		}
//	}
	/**
	 * This method updates the GUI
	 * This method overrides IqmOperatorJFrame
	 */
	@Override
	public void updateIqmOperatorGUI(){
	
	}

    //------------------------------------------------------------------------------------
	/**
	 * This method initializes the Option: Rast
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonRast() {
		if (buttRast == null) {
			buttRast  = new JRadioButtonMenuItem();
			buttRast.setText("Raster");
			buttRast.setToolTipText("uses rasters to match");
			buttRast.addActionListener(this);
			buttRast.setActionCommand("parameter");
		}
		return buttRast;
	}
	/**
	 * This method initializes the Option: Iter
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonIter() {
		if (buttIter == null) {
			buttIter  = new JRadioButtonMenuItem();
			buttIter.setText("Iterator");
			buttIter.setToolTipText("uses iterators to match");
			buttIter.addActionListener(this);
			buttIter.setActionCommand("parameter");
		}
		return buttIter;
	}
	/**
	 * This method initializes the hidden Button option	
	 * 	
	 * @return javax.swing.JRadioButtonMenuItem	
	 */
	private  JRadioButtonMenuItem getJRadioButtonMethodHidden() {
		//if (buttMethodHidden == null) {
			buttMethodHidden  = new JRadioButtonMenuItem();
			buttMethodHidden.setText("Hidden");
			buttMethodHidden.setVisible(false);
		//}
		return buttMethodHidden;
	}
	/**
	 * This method initializes jJToolBarBar	
	 * 	
	 * @return javax.swing.JToolBar	
	 */
	private   JToolBar getJToolBarMethod() {
//		if (jToolBarMethod == null) {
			jToolBarMethod = new JToolBar("Method");
			jToolBarMethod.setOrientation(1); //Vertical orientation
			jToolBarMethod.add(getJRadioButtonRast());			
			jToolBarMethod.add(getJRadioButtonIter());					
			//jToolBarMethod.addSeparator();
			this.setButtonGroupMethod();	//Gruppieren der JRadioButtons
//		}
		return jToolBarMethod;
	}
	private  void setButtonGroupMethod() {
		//if (ButtonGroup buttGroupMethod == null) {
			buttGroupMethod = new ButtonGroup();
			buttGroupMethod.add(buttRast);
			buttGroupMethod.add(buttIter);	
	}

	//--------------------------------------------------------------------------------------------
	public void actionPerformed(ActionEvent e) {
		//System.out.println("IqmTempMatchGUI: event e: " +e.getActionCommand());
		if ("reset".equals(e.getActionCommand())) {
			ParameterBlockJAI pb = new ParameterBlockJAI("IqmTempMatch".toLowerCase());
			IqmTools.setCurrParameterBlockJAI(pb);
			this.setParameterBlock(pb);
			this.setParameterValues();
			//this.updateIqmOperatorGUI();  //if necessary here or some lines above
			this.reset();
		}
		if ("parameter".equals(e.getActionCommand())) {
			this.setParameterBlock();
			//this.updateIqmOperatorGUI();  //if necessary here or some lines above
		}
		
		//preview if selected
		if (this.isAutoPreviewSelected()){
			this.showPreview();
		}

	}


	@Override
	public void adjustmentValueChanged(AdjustmentEvent e) {
		Object obE = e.getSource();
	
		this.setParameterBlock();
		//this.updateIqmOperatorGUI();  //if necessary here or some lines above
		
		//preview if selected
		if (this.isAutoPreviewSelected()){
			this.showPreview();
		}
	}
	//-------------------------------------------------------------------------------------------------
	public static void main() {

		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				IqmTempMatchGUI gui = new IqmTempMatchGUI();
				gui.createIqmOperatorGUI();

			}
		});
	}



	
}//END
